<!-- 首页 -->
<template>
  <view class="home-wrap u-m-b-20">
    <!-- 空白页 -->
    <u-no-network @retry="init"></u-no-network>
    <shopro-empty v-if="!hasTemplate" :image="$IMG_URL + '/imgs/empty/template_empty.png'" tipText="暂未找到模板，请前往装修~">
    </shopro-empty>

    <view v-else-if="isConnected && isRefresh" class="content-box">
      <!-- 导航栏 -->
      <home-head :isScorll="isScorll" borderRadius="0" :navTitle="appName" :list="headSwiperList"></home-head>

      <view class="">

        <view class="navs">
          <view class="nav_n" v-for="(item,key) in nav" :key="key" @click="goDetail(key)">
            <image :src="item.image" mode=""></image>
            <view class="text">{{item.title}}</view>
          </view>
        </view>
        <view class="hot" @click="go">
          <image src="../../static/images/hot.png" mode=""></image>
          <u-notice-bar bg-color="white" color="red" mode="vertical" :volume-icon="false" :list="notice">
          </u-notice-bar>
        </view>
      </view>


      <!-- 自定义模块 -->
      <view class="template-box">
        <block v-for="(item, index) in homeTemplate" :key="item.id">
          <!-- 轮播 -->
          <!-- <sh-banner
						v-if="item.type === 'banner' && index !== 0"
						:Px="item.content.x"
						:Py="item.content.y"
						:borderRadius="item.content.radius"
						:height="item.content.height"
						:list="item.content.list"
					></sh-banner> -->

          <!-- 搜索 -->
          <sh-search v-if="item.type === 'search'"></sh-search>

          <!-- 滑动宫格 -->
          <!-- <sh-grid-swiper v-if="item.type === 'menu'" :list="item.content.list" :oneRowNum="item.content.style"></sh-grid-swiper> -->

          <!-- 推荐商品 -->
          <!-- <sh-hot-goods v-if="item.type === 'goods-list' || item.type === 'goods-group'"
						:detail="item.content"></sh-hot-goods> -->
          <!-- 广告魔方 -->
          <!-- <sh-adv v-if="item.type === 'adv'" :detail="item.content"></sh-adv> -->
          <!-- 优惠券 -->
          <!-- <sh-coupon v-if="item.type === 'coupons'" :detail="item.content"></sh-coupon> -->
          <!-- 秒杀-->
          <!-- <sh-seckill v-if="item.type === 'seckill'" :detail="item.content"></sh-seckill> -->
          <!-- 拼购 -->
          <!-- <sh-groupon v-if="item.type === 'groupon'" :detail="item.content"></sh-groupon> -->
          <!-- 富文本 -->
          <!-- <sh-richtext v-if="item.type === 'rich-text'" :richId="item.content.id"></sh-richtext> -->
          <!-- 功能列表 -->
          <!-- <sh-cell v-if="item.type === 'nav-list'" :list="item.content.list"></sh-cell> -->
          <!-- 九宫格列表 -->
          <!-- <sh-grid v-if="item.type === 'grid-list'" :detail="item.content.list"></sh-grid> -->
          <!-- 功能标题 -->
          <!-- <sh-title-card v-if="item.type === 'title-block'" :title="item.content.name" :bgImage="item.content.image" :titleColor="item.content.color"></sh-title-card> -->
          <!-- 钱包 -->
          <!-- <sh-wallet v-if="item.type === 'wallet-card'"></sh-wallet> -->
          <!-- 订单卡片 -->
          <!-- <sh-order-card v-if="item.type === 'order-card'"></sh-order-card> -->
          <!-- 直播 -->
          <!-- #ifdef MP-WEIXIN -->
          <!-- <sh-live v-if="item.type === 'live' && HAS_LIVE" :detail="item.content"></sh-live> -->
          <!-- #endif -->
        </block>
      </view>

      <!-- 积分兑换 -->
      <view class="hots">

        <view class="hots_goods">
          <view class="hots_title">
            <span class="left"> 积分兑换 </span>
            <span class="right" @click="golist()"> 更多福利 > </span>
          </view>
          <view class="hots_goods1">
            <view class="hots_goodss" v-for="(item,index) in goods" :key="index" @click="tiao(item.id)">
              <view class="image">
                <image :src="item.image" mode=""></image>
              </view>
              <view class="title">{{item.title}}</view>
              <view class="price">
                <span>{{item.score}}积分</span>

              </view>
            </view>
          </view>

        </view>
      </view>

      <!-- 选项卡切换 -->
      <view class="end-title">
        <view v-for="(item,index) in items" :key="index" :class="{btna:count == item.id}" @tap="change(item.id)">
          {{item.title}}
        </view>
      </view>
      <view class="end-cont">

        <dl class="main" v-for="(item,index) in newList" :key="index" @click="jump(item.id)">
          <dt>
            <image :src="item.image" mode=""></image>
          </dt>
          <dd style="margin-top: 10rpx;">{{item.title}}</dd>
          <dd class="time">{{item.createtime}}</dd>
        </dl>

      </view>



      <!-- 分类选项卡 -->
      <!-- <sh-category-tabs
				v-if="categoryTabsData && categoryTabsData.category_arr && categoryTabsData.category_arr.length"
				:enable="enable" :styleType="categoryTabsData.style" :tabsList="categoryTabsData.category_arr">
			</sh-category-tabs> -->
      <!-- 登录提示 -->
      <shopro-auth-modal></shopro-auth-modal>
      <!-- 隐私协议 -->
      <!-- #ifdef APP-PLUS -->
      <privacy-modal v-if="shopInfo && shopInfo.name" v-model="showPrivacy"></privacy-modal>
      <!-- #endif -->
      <!-- #ifdef H5 -->
      <view class="tabbar-hack" style="height: 120rpx; width:100%;"></view>
      <!-- #endif -->
    </view>
    <view class="alert" v-if="show" @touchmove.stop.prevent="moveStop">
      <view class="alert-img">
        <image src="../../static/alert.png"></image>
      </view>
      <scroll-view scroll-y="true" class="scoll" style="height: 300rpx;">
        <view style="padding: 0 20rpx;" v-html="salert"></view>
      </scroll-view>
    </view>
    <view class="bg" v-if="show" @click="show = false" @touchmove.stop.prevent="moveStop"></view>
  </view>
</template>

<script>
  import shBanner from './components/sh-banner.vue';
  import shGridSwiper from './components/sh-grid-swiper.vue';
  import shHotGoods from './components/sh-hot-goods.vue';
  import shAdv from './components/sh-adv.vue';
  import shCoupon from './components/sh-coupon.vue';
  import shSeckill from './components/sh-seckill.vue';
  import shGroupon from './components/sh-groupon.vue';
  import shRichtext from './components/sh-richtext.vue';
  import shCell from './components/sh-cell.vue';
  import shGrid from './components/sh-grid.vue';
  import shTitleCard from './components/sh-title-card.vue';
  import shOrderCard from './components/sh-order-card.vue';
  import shWallet from './components/sh-wallet.vue';
  import shSearch from './components/sh-search.vue';
  import shCategoryTabs from './components/sh-category-tabs.vue';
  import {
    mapMutations,
    mapActions,
    mapState
  } from 'vuex';

  import privacyModal from './index/privacy-modal.vue';
  import homeHead from './index/home-head.vue';

  // #ifdef MP-WEIXIN
  import {
    HAS_LIVE
  } from '@/env';
  import shLive from './components/sh-live.vue';
  // #endif

  // #ifdef H5
  let listenMove = document.body; //禁止手机h5下拉刷新带动整个页面。
  let handle = function(e) {
    e.preventDefault();
  };
  // #endif

  export default {
    components: {
      shBanner,
      shGridSwiper,
      shGroupon,
      shHotGoods,
      shAdv,
      shCoupon,
      shSeckill,
      shRichtext,
      shCell,
      shGrid,
      shTitleCard,
      shWallet,
      shOrderCard,
      shSearch,
      shCategoryTabs,

      privacyModal,
      homeHead,

      // #ifdef MP-WEIXIN
      shLive
      // #endif
    },
    data() {
      return {
        // #ifdef MP-WEIXIN
        HAS_LIVE: HAS_LIVE,
        // #endif
        isRefresh: true,
        enable: false, //是否开启吸顶。
        isConnected: true, //是否有网
        showPrivacy: false, //协议
        isScorll: false,


        items: [],
        count: 2,
        newList: [],
        goods: [],
        notice: [],
        show: false,
        salert: ''
      };
    },
    computed: {
      ...mapState({
        config: ({
          shopro
        }) => shopro.config,
        shopInfo: ({
          shopro
        }) => shopro.config?.shop,
        isLogin: ({
          user
        }) => user.isLogin,
        homeTemplate: ({
          shopro
        }) => shopro.template,
        hasTemplate: ({
          shopro
        }) => shopro.hasTemplate,
        shop: ({
          shopro
        }) => shopro.config.shop //初始化数据
      }),
      // 项目标题
      appName() {
        if (this.config?.shop) {
          return this.config?.shop?.name;
        }
      },
      // 头部模块数据
      headSwiperList() {
        console.log(this.homeTemplate.lunbo, '轮播')
        return this.homeTemplate.lunbo;
        // }
      },
      // 导航
      nav() {
        return this.homeTemplate.daohang;
      },
      //分类选项卡数据
      categoryTabsData() {
        if (this.homeTemplate?.length) {
          return this.homeTemplate[this.homeTemplate.length - 1]?.content;
        }
      }
    },
    onPullDownRefresh() {
      this.init();
      this.exchange();
    },
    onPageScroll(e) {
      this.isScorll = e.scrollTop > 100 ? true : false;
    },
    mounted() {
      this.setalert();
    },
    onShow() {
      let that = this;
      this.enable = true;
      this.isLogin && this.getCartList();
      // #ifdef APP-VUE
      // 网络变化检测
      uni.onNetworkStatusChange(res => {
        this.isConnected = res.isConnected;
      });
      uni.getNetworkType({
        success: res => {
          res.networkType == 'none' ? (this.isConnected = false) : (this.isConnected = true);
        }
      });
      // #endif
      this.getTemplate();
      // this.getUserInfo();
    },
    onHide() {
      this.enable = false;
    },
    onLoad() {
      this.news();
      this.exchange();
      // #ifdef APP-VUE
      // app隐私协议弹窗
      if (!plus.runtime.isAgreePrivacy()) {
        this.showPrivacy = true;
        this.showNoticeModal = false;
      }
      // #endif
      this.getnotice();
      // this.getUserInfo();
    },
    methods: {
      ...mapActions(['appInit', 'getTemplate', 'getCartList', 'getUserInfo']),
      jweixin() {
        this.$wxsdk.openLocation();
      },
      moveStop(){},
      getnotice() {
        this.$http('common.notice', {}).then(res => {
          // this.notice = res.data;
          for (let x in res.data) {
            this.notice.push(res.data[x].title)
          }
          console.log(this.notice, "+++++++++++")
        });
      },
      news() {
        this.$http('common.news', {
          id: 2
        }).then(res => {
          this.items = res.data.cates;
          this.newList = res.data.data;
        });
      },
      setalert() {
        this.$http('common.alert', {
          id: 2
        }).then(res => {
          this.salert = res.data.replace(/\r/g, '<br/>');
          console.log(this.salert, '弹窗')
          this.show = true;
        });
      },
      // 初始化
      init() {
        this.isRefresh = false;
        return Promise.all([this.getTemplate()]).then(() => {
          uni.stopPullDownRefresh();
          this.isRefresh = true;
        });
      },
      go() {
        this.$Router.push({
          path: '/pages/public/tip/list',
          query: {
            type: 'notice'
          },
        })
      },
      tiao(id) {
        this.$Router.push({
          path: '/pages/goods/detail',
          query: {
            id: id
          }
        })
      },
      golist() {
        this.$Router.push({
          path: '/pages/activity/groupon/list'
        })
      },
      goDetail(e) {
        var that = this;
        console.log(e);
        if (e == 0) {
          that.about();
        }
        if (e == 1) {
          that.play();
        }
        if (e == 2) {
          that.help();
        }
        if (e == 3) {
          that.customer();
        }

      },
      about() {
        // this.$Router.push({
        //   path: '/pages/public/richtext',
        //   query: {
        //     id: 3
        //   }
        // })
        uni.navigateTo({
          url:'/pages/public/richtext?id=3'
        })
      },
      play() {
        this.$Router.push({
          path: '/pages/public/play/list',
          query: {
            type: 1,

          }
        })
        // this.$Router.push('/pages/public/play/list')
      },
      help() {
        this.$Router.push({
          path: '/pages/public/play/list',
          query: {
            type: 2,
          }
        })
      },
      customer() {
        this.$Router.push('/pages/public/chat/lineService')

      },
      // 积分兑换
      exchange() {
        let that = this;
        that.$http('common.exchange', {

        }).then(res => {
          this.goods = res.data
        });
      },
      change(e) {
        this.count = e

        let that = this;
        that.$http('common.news', {
          id: e
        }).then(res => {
          this.newList = res.data.data
        });
        console.log(this.count)
      },
      jump(e) {
        this.$Router.push({
          path: '/pages/public/play/detail',
          query: {
            id: e,
            type: 2
          }
        })
      }

    }
  };
</script>

<style lang="scss">
  .navs {
    justify-content: flex-end;
    display: flex;
    width: 100%;
    background: white;

    .nav_n {
      justify-content: space-around;
      width: 25%;
      text-align: center;
      margin-top: 10rpx;

      image {
        width: 60rpx;
        height: 60rpx;
      }

      .text {
        // padding: 20rpx 0;
        font-size: 20rpx;
        padding-bottom: 4rpx;
      }
    }
  }

  .hot {
    background: white;
    overflow: hidden;
    border-top: 2rpx solid #F7F7F7;
    border-bottom: 25rpx solid #F7F7F7;

    image {
      width: 143rpx;
      height: 31rpx;
      float: left;
      padding: 20rpx 0rpx 20rpx 50rpx;
    }

    u-notice-bar {
      width: 300rpx;
      float: left;
      font-size: 26rpx;
      // margin-top:20rpx;
    }
  }

  .end-title {
    display: flex;
    background-color: white;
  }

  .end-title view {
    flex-grow: 1;
    text-align: center;
    padding: 20rpx 0;
  }

  .end-cont {
    overflow: hidden;
    background-color: white;

    dl {
      margin-top: 30rpx;
      overflow: hidden;
      border-bottom: 1rpx solid #DADADA;
      padding: 18rpx 60rpx;
    }

    image {
      width: 201rpx;
      height: 129rpx;
      border-radius: 10rpx;
      float: right;
      margin-bottom: 10rpx;
    }

    .time {
      font-size: 20rpx;
      color: #999999;
      margin-top: 30rpx;
    }

    dd {
      padding-right: 15rpx;
    }
  }

  .btna {
    // margin:0 50rpx;
    border-bottom: 5rpx solid #F9A81F;
    // color: #FFFFFF;
    // background: #00A0FF;
  }


  .hots {
    background: white;
    border-bottom: 30rpx solid #F4F4F4;
    padding-top: 10rpx;

    .hots_goods {
      margin: 30rpx;
      background: url(../../static/images/hot_goods.png) no-repeat;
      background-size: 100% 100%;
      padding-bottom: 20rpx;

      .hots_title {
        overflow: hidden;
        padding: 20rpx;

        .left {
          float: left;
          font-size: 30rpx;
          font-weight: bold;
          color: white;
        }

        .right {
          font-size: 20rpx;
          float: right;
          color: white;
        }
      }

      .hots_goods1 {
        display: flex;
        justify-content: space-between;
        background: white;
        margin: 20rpx;
        padding: 30rpx;
        border-radius: 20rpx;

        .hots_goodss {
          width: 164rpx;
          font-size: 16rpx;

          image {
            width: 164rpx;
            height: 164rpx;
          }

          .title {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }

          .price {
            color: #F9A81F;
          }
        }
      }

    }
  }

  .alert {
    width: 65%;
    border-radius: 40rpx;
    position: fixed;
    top: 50%;
    margin-left: 17.5%;
    left: 0;
    z-index: 501;
    .alert-img {
      position: absolute;
      top: -200rpx;
      width: 100%;
      height: 200rpx;
      background: linear-gradient(to right, #FA6E3F, #FEAB55);
      border-top-left-radius: 10px;
      border-top-right-radius: 10px;
      image {
        width: 100%;
        height: 250rpx;
        position: relative;
        top: -50rpx;
      }
    }
    .scoll{
      background-color: #ffffff;
      border-bottom-left-radius: 10px;
      border-bottom-right-radius: 10px;
    }
  }

  .bg{
    width: 100%;
    height: 100vh;
    background-color: rgba($color: #000000, $alpha: 0.4);
    z-index: 500;
    position: absolute;
    left: 0;
    top: 0;
  }
</style>
